const app = getApp()

Component({
  properties: {},
  data: {
    userInfo: {},
  },
  // pageLifetimes: {
  //   async show () {
  //     if (!app.globalData.userInfo) {
  //       const res = await app.helper.getUserInfo('/wxa/user/login')
  //       app.globalData.userInfo = res
  //     }
  //     this.setData({ userInfo: app.globalData.userInfo })
  //     console.log(this.data.userInfo)
  //   }
  // },
  async ready () {
    if (!app.globalData.userInfo) {
      const res = await app.helper.getUserInfo('/wxa/user/login')
      app.globalData.userInfo = res
    }
    this.setData({ userInfo: app.globalData.userInfo })
  },
  methods: {
    async bindGetUserInfo () {
      const res = await wx.getUserProfile({ desc: '用于完善会员资料' })
      // console.log(res.userInfo)
      // app.globalData.userInfo = {}
      app.globalData.userInfo.wx_nickname = res.userInfo.nickName
      app.globalData.userInfo.wx_headimgurl = res.userInfo.avatarUrl
      this.setData({ userInfo: app.globalData.userInfo })
      // console.log(app.globalData.userInfo)
      await app.http.get('/wxa/user/updateUser', {
        wx_openid: app.globalData.userInfo.wx_openid,
        wx_openid: app.globalData.userInfo.wx_openid,
        wx_nickname: app.globalData.userInfo.wx_nickname,
        wx_headimgurl: app.globalData.userInfo.wx_headimgurl
      })
    },
    async getPhoneNumber (e) {
      await app.helper.getUserInfo('/wxa/user/login')
      const res = await app.http.post('/wxa/user/info', {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        'wxa-sessionid': app.globalData.userInfo['wxa-sessionid']
      })
      if (res.code == 0) {
        app.globalData.userInfo.wx_phone = res.data.wx_phone
        this.setData({ userInfo: app.globalData.userInfo })
      } else {
        app.helper.msg(res.message)
      }
    }
  }
})
